//
//  ShopFormViewController.m
//  MoneyBook
//
//  Created by MyoungHo Kim on 11. 7. 8..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ShopFormViewController.h"

@implementation ShopFormViewController
@synthesize shopTitle = _shopTitle;

@synthesize shopId = _shopId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                  target:self
                                                                                  action:@selector(saveAction)];
        [self.navigationItem setRightBarButtonItem:rightBtn];
        [rightBtn release];
    }
    return self;
}

- (void)dealloc{
    [_shopTitle release];
    [_shopId release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
    _shopTitle.borderStyle = UITextBorderStyleRoundedRect;
    _shopTitle.textColor = [UIColor blackColor];
    _shopTitle.font = [UIFont systemFontOfSize:15.0];
    _shopTitle.backgroundColor = [UIColor whiteColor];
    _shopTitle.autocorrectionType = UITextAutocorrectionTypeNo;
    _shopTitle.keyboardType = UIKeyboardTypeDefault;
    _shopTitle.returnKeyType = UIReturnKeyDone;
    _shopTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
    _shopTitle.placeholder = @"이름을 입력 해 주세요.";
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [self setShopTitle:nil];
    [self setShopId:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UIBarButtonshopDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveAction{
    if(_isCreateMode){
        [[SQLHandler sharedInstance] createShopWithTitle:[_shopTitle text]];
    }else{
//        NSLog(@"saveActoin:  %@",  [_shopTitle text]);
        [[SQLHandler sharedInstance] updateShopId:_shopId 
                                            title:[_shopTitle text]];
    }
    [[self navigationController] popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadShop:(NSNumber*)shopId{
    _isCreateMode = NO;
    NSDictionary *dic = [[SQLHandler sharedInstance] findShopById:shopId];
    //    NSLog(@"dic:%@",dic);
    self.shopId = shopId;
    [self.shopTitle setText:[dic objectForKey:@"title"]];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)createMode{
    _isCreateMode = YES;    
}
@end
